import util from '../../../utils/util.js';
import $ from '../../../utils/ajax.js';
import Base64 from '../../../utils/base64.js';
const app = getApp();
Page({
    data: {
        tel: '',
        code: '',
        pass: '',
        passPlural: '',
        codeObject: {
            codeTime: 60,
            codeText: '',
            codeBtn: false
        },
        setInter: '',
        localRandom: "DontTellOtherpeopleplzas1238fdja0hghn",
        localRandomes: 'M!vw@lFNcOv17Us*yTvyRBHXfbjztDQ1py$*C^w46aRjbOmW3Rc2%MkB38hdO1hpkENwW7^UJ#0foKSH&4ZQX5@VfdASrCLxhG%',
        phone: '',
        encryption: '',
        encryptiones: '',
        helptips: false,
        stateOne: false,
        stateTwo: false,
        stateThree: false,
        stateFour: false,
        ticket: '',
        userPhone: '',
        agentId: '',
        send: true,
        checked: false,
        id: 0
    },
    onLoad(options) {},
    phoneChange(e) {
        let tel = e.detail.value;
        this.setData({
            tel: tel,
        })

    },
    //协议选择
    radioChange(e) {
        this.setData({
            checked: true
        })
    },
    codeBtn(e) {
        const tel = this.data.tel;
        const localRandom = this.data.localRandom;
        let phone = util.Encrypts(tel, localRandom);
        console.log(phone, "手机号加密")
        this.setData({
            phone: phone
        })
        if (util.telCheck(tel, '')) {

            this.setData({
                stateOne: true,
                send: false
            })
            $.p(app.wxApp.url + 'passport/verifyCode', {
                a: this.data.phone,
            }, app.onStrart, (res) => {
                util.showToast('发送成功')
                this.data.setInter = setInterval(() => {
                    let time = this.data.codeObject.codeTime - 1;
                    let text = time + 's';
                    let btn = true;
                    if (time === 0) {
                        time = 60;
                        text = '获取验证码';
                        btn = false;
                        this.setData({
                            send: true
                        })
                        clearInterval(this.data.setInter);
                    }
                    this.setData({
                        codeObject: {
                            codeTime: time,
                            codeText: text,
                            codeBtn: btn
                        }
                    })
                }, 1000)
            }, (res) => {
                util.showToast(res.data)
            })
        } else {
            this.setData({
                send: true
            })
        }
    },
    codeChange(e) {
        let code = e.detail.value
        if (code.length === 0) {
            util.showToast('验证码不能为空')
        } else if (code.length > 6) {
            util.showToast('验证码长度有误')
        } else {
            this.setData({
                code: code,
                stateTwo: true
            })
        }
    },
    passChange(e) {
        // console.log(e.detail.value,"我是你叭叭叭")
        let newPas = e.detail.value;
        if (util.passWord(newPas)) {
            this.setData({
                pass: newPas,
                stateThree: true
            })
        } else if (!util.passWord(newPas)) {

            this.setData({
                pass: "",
                stateThree: false
            })
            // console.log(this.data.pass,"aaaaaaa")
        }
    },
    passPluralChange(e) {
        let passPlural = e.detail.value;
        this.setData({
            passPlural: passPlural
        })
    },
    modify() {

        const pass = this.data.pass;
        const passPlural = this.data.passPlural;
        // console.log(pass,"注册1")
        // console.log(passPlural, "注册2")
        if (pass != passPlural) {
            util.showToast('两次密码不一致')
            this.setData({
                pass: "",
                passPlural: "",
                stateFour: false
            })
            // console.log(this.data.pass, "初始密码12");
            return
        } else if (pass === passPlural) {
            this.setData({
                stateFour: true
            })
        }

        // console.log(pass,"11")
        // console.log(passPlural, "22")
        // console.log(this.data.stateOne,"1")
        // console.log(this.data.stateTwo,"2")
        // console.log(this.data.stateThree,"3")
        // console.log(this.data.stateFour,'4')
        const localRandomes = this.data.localRandomes;
        let passWord = util.Encrypt(pass, localRandomes);
        let passWordAgain = util.Encrypt(passPlural, localRandomes);
        let stateOne = this.data.stateOne;
        let stateTwo = this.data.stateTwo;
        let stateThree = this.data.stateThree;
        let stateFour = this.data.stateFour;

        if (stateOne && stateTwo && stateThree && stateFour) {
            $.p(app.wxApp.url + 'passport/getServerRandom', {
                mobileNo: this.data.phone
            }, app.onStrart, (res) => {
                console.log(res, "获取的加密因子")
                let base64 = Base64.base64_decode(res.data.serverRandom)
                console.log(base64, '解密加密因子')
                const encryption = util.Encrypt(passWord, base64)
                console.log(encryption, "最后发送给后台的密码1")
                const encryptiones = util.Encrypt(passWordAgain, base64)
                console.log(encryptiones, "最后发送给后台的密码2")
                this.setData({
                    encryption: encryption,
                    encryptiones: encryptiones
                })
                app.onShop();
                //把加密后的密码发送给后端
                // app.wxApp.url
                $.p(app.wxApp.url + 'agentNew/agentRegister', {
                    mobileNo: this.data.phone,
                    password: this.data.encryption,
                    rePassword: this.data.encryptiones,
                    code: this.data.code,
                }, app.onStrart, (res) => {
                    app.onShop();
                    if (res.status === 0) {
                        this.setData({
                            helptips: true,
                            ticket: res.data.ticket,
                            userPhone: res.data.userPhone,
                            agentId: res.data.agentId
                        })

                    }
                }, (res) => {
                    util.showToast(res.data)
                })
            }, (res) => {
                util.showToast(res.data)
            })

        } else {
            util.showToast('注册信息填写有误')
        }
    },

    //稍后认证
    later() {
        wx.reLaunch({
            url: '/pages/login/login'
        })
    },
    //立即认证
    attestation() {
        wx.setStorageSync('xfjtk', this.data.ticket);
        wx.setStorageSync('tel', this.data.userPhone);
        wx.setStorageSync('agent', this.data.agentId);
        wx.reLaunch({
            url: '/pages/login/agent/agent'
        })
    },
    ui(e) {
        console.log(e, "111")
        const id = e.currentTarget.dataset.id
        this.setData({
            id: id == 1 ? '2' : '1'
        })
        const ui = this.data.id
        if (ui == 1) {
            this.setData({
                checked: true
            })
        } else {
            this.setData({
                checked: false
            })
        }
    },

    onPdf() {
        wx.showLoading({
            mask: true,
            title: '协议下载中'
        })
        wx.downloadFile({
            url: 'https://xfj.oss-cn-shanghai.aliyuncs.com/protocol/register.pdf',
            success: function (res) {
                const filePath = res.tempFilePath;
                setTimeout(() => {
                    wx.openDocument({
                        filePath: filePath,
                        fileType: 'pdf',
                        success: function (res) {
                            console.log(res)
                        }
                    })
                }, 100);
                wx.hideLoading();
            }
        })
    },
})